package com.tebiecloud.yunst.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.yunst.client.constants.ResultEnum;
import com.tebiecloud.yunst.client.model.entity.YunBankCard;
import com.tebiecloud.yunst.client.model.entity.YunStUser;
import com.tebiecloud.yunst.client.model.entity.YunWithdrawApply;
import com.tebiecloud.yunst.client.model.vo.WithdrawInfoVO;
import com.tebiecloud.yunst.server.service.YunAgentPayService;
import com.tebiecloud.yunst.server.service.YunBankCardService;
import com.tebiecloud.yunst.server.service.YunStUserService;
import com.tebiecloud.yunst.server.service.YunWithdrawApplyService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.yunst.server.yunstservice.YunStService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-09-07
 */
@Slf4j
@RestController
@RequestMapping("/yunwithdrawapply")
public class YunWithdrawApplyController {

    @Autowired
    private YunWithdrawApplyService yunWithdrawApplyService;

    @Autowired
    private YunBankCardService yunBankCardService;

    /**
     * 根据ID查找数据
     * @param withdrawId
     * @return
     */
    @PostMapping("withdrawInfo")
    public ResultBody withdrawInfo(@RequestParam("withdrawId") Long withdrawId) {
        WithdrawInfoVO withdrawInfoVO = new WithdrawInfoVO();
        YunWithdrawApply yunWithdrawApply = yunWithdrawApplyService.getById(withdrawId);
        if(yunWithdrawApply==null){
            return ResultBodyUtils.failed("查询提现信息不存在");
        }
        //查询银行卡
        QueryWrapper<YunBankCard> yunBankCardQueryWrapper = new QueryWrapper<>();
        yunBankCardQueryWrapper.eq("card_no",yunWithdrawApply.getBankCardNo());
        yunBankCardQueryWrapper.eq("user_id",yunWithdrawApply.getUserId());
        YunBankCard yunBankCard = yunBankCardService.getOne(yunBankCardQueryWrapper);
        if(yunBankCard==null){
            log.info(">>minjie 该用户的银行卡查询不存在：userId：{}，卡号：{}",yunWithdrawApply.getUserId(),yunWithdrawApply.getBankCardNo());
            return ResultBodyUtils.failed("银行卡查询不存在");
        }
        withdrawInfoVO.setBank(yunBankCard.getBank());
        withdrawInfoVO.setName(yunBankCard.getName());
        withdrawInfoVO.setNumber(withdrawId.toString());
        //实际到账的金额
        withdrawInfoVO.setAmount(yunWithdrawApply.getAmount()-yunWithdrawApply.getFee());
        int length = yunWithdrawApply.getBankCardNo().length();
        withdrawInfoVO.setCardNo(yunWithdrawApply.getBankCardNo().substring(length-4,length));
        withdrawInfoVO.setFee(yunWithdrawApply.getFee());
        withdrawInfoVO.setTax(yunWithdrawApply.getTax());
        withdrawInfoVO.setApplyTime(yunWithdrawApply.getApplyTime());
        withdrawInfoVO.setArrivedTime(yunWithdrawApply.getArrivedTime());
        withdrawInfoVO.setStatus(yunWithdrawApply.getStatus());
        return ResultBodyUtils.ok(withdrawInfoVO);
    }





//    /**
//     * 获取分页数据
//     * @param map
//     * @return
//     */
//    @GetMapping(value = "list")
//    public ResultBody<IPage<YunWithdrawApply>> list(@RequestParam(required = false) Map map) {
//        PageParams pageParams = new PageParams(map);
//        YunWithdrawApply query = pageParams.mapToObject(YunWithdrawApply.class);
//        QueryWrapper<YunWithdrawApply> queryWrapper = new QueryWrapper();
//        return ResultBodyUtils.ok(yunWithdrawApplyService.page(new PageParams(map), queryWrapper));
//    }
//
//    /**
//     * 根据ID查找数据
//     * @param id
//     * @return
//     */
//    @GetMapping("get")
//    public ResultBody<YunWithdrawApply> get(@RequestParam("id") Long id) {
//        YunWithdrawApply entity = yunWithdrawApplyService.getById(id);
//        return ResultBodyUtils.ok(entity);
//    }
//
//    /**
//     * 添加数据
//     * @param bizUserId 提现人id,
//     * @param amount 金额（单位：分）,
//     * @param fee 手续费（单位分）,
//     * @param bankCardNo 银行卡号,
//     * @param status 提现状态：0申请中，1成功，2失败,
//     * @param applyTime 申请时间,
//     * @param arrivedTime 到账时间,
//     * @param remark
//     * @return
//     */
//    @PostMapping("add")
//    public ResultBody add(
//        @RequestParam(value = "bizUserId", required = false) String bizUserId,
//        @RequestParam(value = "amount", required = false) Integer amount,
//        @RequestParam(value = "fee", required = false) Integer fee,
//        @RequestParam(value = "bankCardNo", required = false) String bankCardNo,
//        @RequestParam(value = "status", required = false) Integer status,
//        @RequestParam(value = "applyTime", required = false) Date applyTime,
//        @RequestParam(value = "arrivedTime", required = false) Date arrivedTime,
//        @RequestParam(value = "remark", required = false) String remark
//    ) {
//        YunWithdrawApply entity = new YunWithdrawApply();
//        entity.setBizUserId(bizUserId);
//        entity.setAmount(amount);
//        entity.setFee(fee);
//        entity.setBankCardNo(bankCardNo);
//        entity.setStatus(status);
//        entity.setApplyTime(applyTime);
//        entity.setArrivedTime(arrivedTime);
//        entity.setRemark(remark);
//        yunWithdrawApplyService.save(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 更新数据
//     * @param bizUserId 提现人id
//     * @param amount 金额（单位：分）
//     * @param fee 手续费（单位分）
//     * @param bankCardNo 银行卡号
//     * @param status 提现状态：0申请中，1成功，2失败
//     * @param applyTime 申请时间
//     * @param arrivedTime 到账时间
//     * @param remark
//     * @return
//     */
//    @PostMapping("update")
//    public ResultBody update(
//            @RequestParam(value = "withdrawId", required = false) Long withdrawId,
//            @RequestParam(value = "bizUserId", required = false) String bizUserId,
//            @RequestParam(value = "amount", required = false) Integer amount,
//            @RequestParam(value = "fee", required = false) Integer fee,
//            @RequestParam(value = "bankCardNo", required = false) String bankCardNo,
//            @RequestParam(value = "status", required = false) Integer status,
//            @RequestParam(value = "applyTime", required = false) Date applyTime,
//            @RequestParam(value = "arrivedTime", required = false) Date arrivedTime,
//            @RequestParam(value = "remark", required = false) String remark
//    ) {
//        YunWithdrawApply entity = new YunWithdrawApply();
//        entity.setWithdrawId(withdrawId);
//        entity.setBizUserId(bizUserId);
//        entity.setAmount(amount);
//        entity.setFee(fee);
//        entity.setBankCardNo(bankCardNo);
//        entity.setStatus(status);
//        entity.setApplyTime(applyTime);
//        entity.setArrivedTime(arrivedTime);
//        entity.setRemark(remark);
//        yunWithdrawApplyService.updateById(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 删除数据
//     * @param id 单个id
//     * @param ids 多个id用,号隔开
//     * @return
//     */
//    @PostMapping("remove")
//    public ResultBody remove(
//            @RequestParam(value = "id", required = false) Long id,
//            @RequestParam(value = "ids", required = false) String ids
//    ) {
//        if(id != null && id > 0) {
//            yunWithdrawApplyService.removeById(id);
//        }
//        if(ids != null && ids.length() > 0) {
//            yunWithdrawApplyService.removeByIds(Arrays.asList(ids.split(",")));
//        }
//        return ResultBodyUtils.ok();
//    }

}
